package com.gxa.worker.recycling.interceptor;


import com.gxa.core.recycling.anno.NeedToken;
import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.Recycler;
import com.gxa.core.recycling.util.JsonUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;

/**
 * @author HS
 */
@Component("permissionInterceptor")
public class PermissionInterceptor implements HandlerInterceptor {

    /**
     * 在方法执行前执行的操作
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        /**
         * 通过@NeedToken注解升级
         */
        //从方法中获得@NeedToken注解(通过反射获取注解信息)
        //通过强转,将object类型的handler参数转为反射可以操作的HandlerMethod(Method)

        String requestURI = request.getRequestURI();
        System.out.println(requestURI);


        HandlerMethod method=(HandlerMethod) handler;

        boolean b = method.hasMethodAnnotation(NeedToken.class);

        if(!b){
            System.out.println("没有标记不拦截");
            return true;
        }else {
            NeedToken methodAnnotation = method.getMethodAnnotation(NeedToken.class);
            boolean required = methodAnnotation.required();
            if(!required){
                System.out.println("有标记但不拦截");
                return true;
            }else {

                System.out.println("有标记拦截");
                //return false;
                String token = request.getHeader("token");
//
                System.out.println(token);

                RestTemplate restTemplate = new RestTemplate();
                //指定uri地址
                URI uri = new URI("http://47.108.224.187:8090/sso/findRecyclerByToken");

                // 参数, 用Spring封装的MultiValueMap
                MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
                paramMap.add("token",token);
                //调用postForObject方法, 指定uri/参数/返回值类型
                // 返回值如果是一个json字符串, 就可以用JsonUtil来解析
                String responseString  = restTemplate.postForObject(uri, paramMap, String.class);

                System.out.println(responseString);
                //把json字符串转换为User对象
                Recycler recycler = JsonUtil.readToObject(responseString, Recycler.class);
                if (recycler!=null) {
                    return true;
                } else {
                    throw new BusinessException(ExceptionCode.WRONG_TOKEN);
                }
            }
        }




    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 在方法执行后执行的操作(指放行后)
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
